package com.cjm.one.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.custom.mapper.CustomerInfoMapper;
import com.cjm.one.custom.mapper.CustomGradeMapper;
import com.cjm.one.custom.service.CustomGradeService;
import com.cjm.one.model.custom.entity.CustomGrade;
import com.cjm.one.model.custom.entity.CustomerInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author 29289
* @description 针对表【custom_grade】的数据库操作Service实现
* @createDate 2024-11-04 16:20:19
*/
@Service
public class CustomGradeServiceImpl extends ServiceImpl<CustomGradeMapper, CustomGrade>
    implements CustomGradeService {
    @Resource
    private CustomGradeMapper customGradeMapper;
    @Resource
    private CustomerInfoMapper customerInfoMapper;

    @Override
    public Boolean removeByIdGrade(Long id) {
        CustomGrade customGrade = customGradeMapper.selectById(id);
        if (customGrade != null) {
            LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CustomerInfo::getUserGrade, customGrade.getGradeNam()).last("limit 1");
            CustomerInfo customerInfo = customerInfoMapper.selectOne(queryWrapper);
            if (customerInfo != null) {
                throw  new GlobalException(ResultCodeEnum.DELETE_GRADE_ERROR);
            }
            return customGradeMapper.deleteById(id) > 0;
        }
        return false;
    }

    @Override
    public Boolean updateStatusById(Long id, Integer status) {

        CustomGrade customGrade = customGradeMapper.selectById(id);
        if (customGrade == null) {
            return false;
        }
        LambdaUpdateWrapper<CustomGrade> customGradeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        customGradeLambdaUpdateWrapper.eq(CustomGrade::getId, id);
        customGradeLambdaUpdateWrapper.set(CustomGrade::getStatus, status);
        return customGradeMapper.update(customGrade, customGradeLambdaUpdateWrapper) > 0;

    }
}




